/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.entity.SysDept;
import com.pig4cloud.pigx.common.core.constant.enums.BooleanTypeEnum;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.order.api.entity.OrderInfo;
import com.pig4cloud.pigx.order.api.vo.OrderInfoVO;
import com.pig4cloud.pigx.shop.api.entity.PointSpu;
import com.pig4cloud.pigx.shop.api.entity.PointSpuCategory;
import com.pig4cloud.pigx.shop.api.vo.PointSpuVO;
import com.pig4cloud.pigx.shop.mapper.PointSpuMapper;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import com.pig4cloud.pigx.shop.service.PointSpuCategoryService;
import com.pig4cloud.pigx.shop.service.PointSpuService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 积分商品
 *
 * @author pigx code generator
 * @date 2023-12-18 17:29:21
 */
@Service
@AllArgsConstructor
public class PointSpuServiceImpl extends ServiceImpl<PointSpuMapper, PointSpu> implements PointSpuService {

	private final PointSpuCategoryService pointSpuCategoryService;

	private final GoodsMaterialService materialService;

	/**
	 * 分页获取商城商品数据
	 * @param page
	 * @param pointSpu
	 * @return
	 */
	@Override
	public IPage getPage(Page page, PointSpu pointSpu) {
		IPage<PointSpuVO> result = new Page<>();
		LambdaQueryWrapper<PointSpu> lqwPointSpu = new LambdaQueryWrapper<>();
		if (pointSpu.getCategoryId() != null) {
			lqwPointSpu.eq(PointSpu::getCategoryId, pointSpu.getCategoryId());
		}
		if (!StringUtils.isEmpty(pointSpu.getStatus())) {
			lqwPointSpu.eq(PointSpu::getStatus, pointSpu.getStatus());
		}
		lqwPointSpu.orderBy(true, true, PointSpu::getSort);
		lqwPointSpu.eq(PointSpu::getDelFlag, DeleteFlagEnum.VALID.getType());
		IPage<PointSpu> items = this.page(page, lqwPointSpu);

		if (items.getRecords().size() > 0) {
			List<Long> ids = new ArrayList<>();
			result = items.convert(u -> {
				PointSpuVO v = new PointSpuVO();
				BeanUtils.copyProperties(u, v);
				ids.add(v.getCategoryId());
				return v;
			});

			// 去重
			List<Long> categoryIds = ids.stream().distinct().collect(Collectors.toList());
			List<PointSpuCategory> pointSpuCategories = pointSpuCategoryService
					.list(Wrappers.<PointSpuCategory>lambdaQuery().in(PointSpuCategory::getId, categoryIds));
			result.getRecords().forEach(item -> {
				Optional<PointSpuCategory> data = pointSpuCategories.stream()
						.filter(p -> p.getId().longValue() == item.getCategoryId().longValue()).findFirst();
				if (data.isPresent()) {
					item.setCategoryName(data.get().getCategoryName());
				}

				// 轮播图
				if (!StringUtils.isEmpty(item.getBannerImages())) {
					item.setBannerImages(materialService.convertImageIdToUrl(item.getBannerImages()));
				}

				// 详情图
				if (!StringUtils.isEmpty(item.getDetailImages())) {
					item.setDetailImages(materialService.convertImageIdToUrl(item.getDetailImages()));
				}
			});

		}
		return result;
	}

}
